package com.cyy.common.domain;

import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.http.HttpStatus;

@Data
@Accessors(chain = true)
public class Response {

    private static final Integer DEFAULT_SUCCESS_CODE = HttpStatus.OK.value();

    private static final String DEFAULT_SUCCESS_MSG = "请求/处理成功！";

    private static final Integer DEFAULT_FAILURE_CODE = HttpStatus.BAD_REQUEST.value();

    private static final String DEFAULT_FAILURE_MSG = "请求/处理失败！";

    /**
     * 响应码
     */
    private Integer code;
    /**
     * 响应提示信息
     */
    private String msg;

    private Object data;

    public Response(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Response(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Response() {
    }

    /**
     * 成功响应
     */
    public static Response success() {
        return new Response(DEFAULT_SUCCESS_CODE, DEFAULT_SUCCESS_MSG);
    }


    /**
     * 失败响应
     */
    public static Response failed() {
        return new Response(DEFAULT_FAILURE_CODE, DEFAULT_FAILURE_MSG);
    }


    public Response msg(String msg) {
        this.msg = msg;
        return this;
    }

    public Response code(Integer code) {
        this.code = code;
        return this;
    }

    public Response code(HttpStatus code) {
        this.code = code.value();
        return this;
    }

    public Response data(Object data) {
        this.data = data;
        return this;
    }
}
